/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dafe.partyband.services.impl.lastfm;

import de.umass.lastfm.Caller;
import de.umass.lastfm.Track;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.dafe.partyband.db.api.IAuthor;
import org.dafe.partyband.db.api.ISong;
import org.dafe.partyband.services.api.IMusicService;
import org.dafe.partyband.services.api.IURIActionDescription;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service=IMusicService.class)
public class LastFMService implements IMusicService {
    
    private static final String API_KEY = "91f57041a85acd076d96ae36a0350f72";
    
    private boolean firstInvocation;

    @Override
    public String getDisplayName() {
        return NbBundle.getMessage(LastFMService.class, "LBL_LastFM");
    }

    @Override
    public String getDisplayHint() {
        return NbBundle.getMessage(LastFMService.class, "HINT_LastFM");
    }

    @Override
    public List<IURIActionDescription<ISong>> getSongURLActionDescriptions() {
        initLastFM();
        
        final IURIActionDescription<ISong> songPageAction = new IURIActionDescription<ISong>() {

            @Override
            public String getName() {
                return NbBundle.getMessage(LastFMService.class, "LBL_SongPage");
            }

            @Override
            public URI getURI(ISong song) {
                Track track = Track.getCorrection(song.getAuthor().getName(), song.getName(), API_KEY);
                if (track == null) {
                    track = Track.getInfo(song.getAuthor().getName(), song.getName(), API_KEY);
                }
                if (track != null) {
                    String trackURL = track.getUrl();
                    if (trackURL != null) {
                        try {
                            return new URI(trackURL);
                        } catch (URISyntaxException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    }
                }
                try {
                    // fallback
                    return new URI("http://www.last.fm");
                } catch (URISyntaxException ex) {
                    Exceptions.printStackTrace(ex);
                }
                
                return null;
            }
        };
        
        return new ArrayList<IURIActionDescription<ISong>>() {{ add(songPageAction); }};
    }

    @Override
    public List<IURIActionDescription<IAuthor>> getAuthorURLActionDescriptions() {
        return Collections.EMPTY_LIST;
    }

    private void initLastFM() {
        if (firstInvocation) {
            firstInvocation = false;
            Caller.getInstance().setUserAgent("tst");
            Caller.getInstance().setDebugMode(true);
        }
    }
    
}
